package com.guogee.chart.handler;

import io.netty.channel.Channel;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.handler.codec.DelimiterBasedFrameDecoder;
import io.netty.handler.codec.Delimiters;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;

public class ServerHandler extends ChannelInitializer {

	@Override
	protected void initChannel(Channel ch) throws Exception {
		//ChannelPipeline 是 ChannelHandler 链的容器。
		//ChannelHandler 实例的链
		ChannelPipeline pipeline = ch.pipeline();
		pipeline.addLast(new DelimiterBasedFrameDecoder(8192, Delimiters.lineDelimiter()));
		pipeline.addLast("decoder", new StringDecoder());
		pipeline.addLast("encoder", new StringEncoder());
		//向pipeline中添加handler
		pipeline.addLast("handler", new SimpleChatServerHandler());
		//用名字添加以后可以remove
//		pipeline.remove("framer");
		System.out.println("SimpleChatClient:" + ch.remoteAddress() + "连接上");
	}
}